﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml.Serialization;
using System.IO;

namespace ClientConclusionListGenerator
{
    public sealed class GeneratorSettingSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            using (TextReader reader = new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + section.OuterXml))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(GeneratorSettings));
                return serializer.Deserialize(reader);
            }
        }
    }


    [XmlRoot("generatorSettings")]
    public sealed class GeneratorSettings
    {
        [XmlArray(ElementName = "sources")]
        [XmlArrayItem(ElementName = "folder", Type = typeof(string))]
        public string[] SrcFolders { get; set; }

        [XmlElement(ElementName = "pattern")]
        public string FilePattern { get; set; }

        [XmlElement(ElementName = "target")]
        public string TargetFolder { get; set; }

        [XmlArray(ElementName = "exclude")]
        [XmlArrayItem(ElementName = "filename", Type = typeof(string))]
        public string[] ExcludeFileNames { get; set; }

        [XmlElement(ElementName = "modifiedTimeAfter", Type = typeof(DateTime))]
        public DateTime? ModifiedTimeAfter { get; set; }
    }

    public static class AppConfig
    {
        private static GeneratorSettings Settings = (GeneratorSettings)ConfigurationManager.GetSection("generatorSettings");

        public static string[] SrcFolders { get { return Settings.SrcFolders; } }

        public static string FilePattern { get { return Settings.FilePattern; } }

        public static string TargetFolder { get { return Settings.TargetFolder; } }

        public static string[] ExcludeFileNames { get { return Settings.ExcludeFileNames; } }

        public static DateTime ModifiedTimeAfter { get { return Settings.ModifiedTimeAfter.HasValue ? Settings.ModifiedTimeAfter.Value : DateTime.MinValue; } }

        //static AppConfig()
        //{
        //    SrcFolders = ConfigurationManager.AppSettings["SrcFolder"].Trim()
        //        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

        //    FilePattern = ConfigurationManager.AppSettings["FilePattern"].Trim();

        //    ExcludeFileNames = ConfigurationManager.AppSettings["excludeFileName"].Trim()
        //        .Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        //    string modifiedTimeAfterStr = ConfigurationManager.AppSettings["ModifiedTimeAfter"].Trim();
        //    DateTime timeAfter;
        //    if (!DateTime.TryParse(modifiedTimeAfterStr, out timeAfter))
        //        timeAfter = DateTime.MinValue;
        //    ModifiedTimeAfter = timeAfter;

        //    TargetFolder = ConfigurationManager.AppSettings["TargetFolder"].Trim();
        //}

        //public static string[] SrcFolders { get; private set; }

        //public static string FilePattern { get; private set; }

        //public static string TargetFolder { get; private set; }

        //public static string[] ExcludeFileNames { get; private set; }

        //public static DateTime ModifiedTimeAfter { get; private set; }
    }
}
